<?php
include_once('Sqlexecutor.php');
class Bulletin
{
    private $_dbh;
    private $_logger;

    public function __construct($dbh, $logger = Null)
    {/*{{{*/
        $this->_dbh = $dbh; 
        $this->_logger = $logger; 
    }/*}}}*/

    public function getInfoByID($id)
    {/*{{{*/
        $sql = "select `id`, `content`, `status`, `insert_date`, `publish_date`, `update_date` from bs_bulletin where id = ?";
        $val[] = $id;
        try
        {
            $info = $this->_dbh->query($sql, $val);
            if($info === false)
                $info = NULL;
            return $info;
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return false;
        }
    }/*}}}*/

    public function getList()
    {/*{{{*/
        $sql = "select `id`, `content`, `status`, `insert_date`, `publish_date`, `update_date` from bs_bulletin order by `id` desc";
        try
        {
            $list = $this->_dbh->querys($sql);
            return $list;
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return false;
        }
    }/*}}}*/

    public function getPublishList()
    {/*{{{*/
        $sql = "select `id`, `content`, `status`, `insert_date`, `publish_date`, `update_date` from bs_bulletin where `status` = 1 order by `publish_date` desc";
        try
        {
            $list = $this->_dbh->querys($sql);
            return $list;
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return false;
        }
    }/*}}}*/

    public function create($content)
    {/*{{{*/
        $sql = "insert into bs_bulletin set `content` = ?, `status` = 0, `publish_date` = '0000-00-00 00:00:00', `insert_date` = now()";
        $val[] = $content;
        try
        {
            $this->_dbh->beginTrans();
            $this->_dbh->execute($sql, $val); 
            $lastId = $this->_dbh->getLastInsertID();
            $this->_dbh->commit();

            return $lastId;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/ 

    public function publishByID($id)
    {/*{{{*/
        $sql = 'update bs_bulletin set `publish_date` = now(), `status` = 1 where `id` = ?';
        $val[] = $id;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            // Unknow error
            return -100;
        }
    }/*}}}*/

    public function updateByID($content, $id)
    {/*{{{*/
        $sql = 'update bs_bulletin set `content` = ? where `id` = ?';
        $val[] = $content;
        $val[] = $id;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/

    public function deleteByID($id)
    {/*{{{*/
        $sql = 'delete from bs_bulletin where `id` = ?';
        $val[] = $id;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not delete, 1 : delete 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            
            // Unknow error
            return -100;
        }
    }/*}}}*/
}

/* example 
$dbh = new Sqlexecutor('localhost', 'tile', 'tile', 'tile');
$s = new Bulletin($dbh);
$res = $s->create("sprin 1ce3876a7f26637ca25d4a026e66.jpg");
var_dump($res);
$res = $s->create("summer 001e5047941dbbaff2f0ca45a4511cc0b.jpg");
var_dump($res);
$res = $s->create("summer 001e5047941dbbaff2f0ca45a4511cc0b.jpg");
var_dump($res);
$res = $s->publishByID($id);
var_dump($res);
$res = $s->getInfoByID(1);
var_dump($res);
$list = $s->getList();
var_dump($list);
$res = $s->updateByID("update summer001e5047941dbbaff2f0ca45a4511cc0b.jpg", 2);
var_dump($res);
$res = $s->deleteByID(3);
var_dump($res);
$list = $s->getList();
var_dump($list);
 */
